package com.douyang.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.douyang.base.expcetion.UserException;
import io.minio.*;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.FilterInputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

@Component
public class MinioUtil {
    @Autowired
    private MinioClient minioClient;

    public boolean checkChunk(String fileMd5,String path) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

        FilterInputStream unloadFile= minioClient.getObject(GetObjectArgs.builder()
                .object(path)
                .bucket("video")
                .build());

        return DigestUtils.md5DigestAsHex(unloadFile).equals(fileMd5);

    }


    public boolean updateChunkFile(MultipartFile chunk,String chunkMd5,String fileMd5) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String path=getBasePath(fileMd5)+chunkMd5;

        minioClient.putObject(PutObjectArgs
                .builder()
                        .bucket("video")
                        .object(path)
                        .stream(chunk.getInputStream(), chunk.getSize(), -1)
                .build());

        return checkChunk(chunkMd5,path);
    }

    public String getBasePath(String md5){
        return md5.charAt(0)+"/"+md5.charAt(1)+"/"+md5.charAt(2)+"/";
    }


    public void mergeChunk(List<String>chunkNameList,String fileMd5,String suffixName) throws Exception{
        String basePath=getBasePath(fileMd5);
        List<ComposeSource> composeSourceList = chunkNameList.stream()
                .map(chunkName ->
                        ComposeSource.builder()
                        .bucket("video")
                        .object(basePath + chunkName)
                        .build()
                )
                .toList();


        minioClient.composeObject(ComposeObjectArgs.builder()
                        .bucket("video")
                        .object(basePath+fileMd5+suffixName)
                        .sources(composeSourceList)
                .build());

        if(!checkChunk(fileMd5,basePath+fileMd5+suffixName)){
            throw new UserException("文件校验失败");
        }
    }

    public String uploadImage(MultipartFile file,String fileMd5) throws Exception{
        String fileName=file.getOriginalFilename();
        if(StringUtils.isEmpty(fileMd5)){fileMd5=".jpg";}
        String basePath=getBasePath(fileMd5)+fileMd5+fileName.substring(fileName.lastIndexOf("."));

        minioClient.putObject(PutObjectArgs.builder()
                        .bucket("image")
                        .object(basePath)
                        .stream(file.getInputStream(), file.getSize(), -1)
                .build());
        return basePath;
    }
}
